/*
 * \brief  Server-side de-marshaller of parent interface
 * \author Norman Feske
 * \date   2006-05-10
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__PARENT__SERVER_H_
#define _INCLUDE__PARENT__SERVER_H_

#include <base/server.h>
#include <parent/parent.h>

namespace Genode {

	class Parent_server : public Server_object, public Parent
	{
		public:

			int dispatch(int op, Ipc_istream &is, Ipc_ostream &os)
			{
				switch (op) {

				case EXIT:
					{
						int exit_value = 0;

						is >> exit_value;
						exit(exit_value);
						return 0;
					}

				case ANNOUNCE:
					{
						Buffer name_buf;
						Root_capability root;

						is >> name_buf >> root;
						return announce(name_buf.addr(), root);
					}

				case SESSION:
					{
						Buffer name_buf, args_buf;

						is >> name_buf >> args_buf;
						try {
							os << session(name_buf.addr(), args_buf.addr());
							return 0;

						/* translate exception types to error codes */
						} catch (Quota_exceeded) {
							os << Session_capability();
							return -1;
						} catch (Service_denied) {
							os << Session_capability();
							return -2;
						}
					}

				case CLOSE:
					{
						Session_capability session;

						is >> session;
						close(session);
						return 0;
					}

				case TRANSFER_QUOTA:
					{
						Session_capability to_session;
						Buffer amount;

						is >> to_session >> amount;
						try {
							transfer_quota(to_session, amount.addr());
							return 0;

						/* translate exception to error code */
						} catch (Quota_exceeded) {
							return -1;
						}
					}

				default: return -1;
				}
			}
	};
}

#endif /* _INCLUDE__PARENT__SERVER_H_ */
